package com.coderdream.chapter12.decorator.a2;

import java.util.Vector;

public class MultiStringDisplay extends Display {

	private Vector body = new Vector();// 打印的字符串

	private int columns = 0; // 最大字數

	/**
	 * 新增字符串
	 * 
	 * @param msg
	 */
	public void add(String msg) {
		body.add(msg);
		this.updateColumn(msg);
	}

	@Override
	public int getColumns() {
		return this.columns;
	}

	@Override
	public int getRows() {
		return this.body.size();
	}

	@Override
	public String getRowText(int row) {
		return (String) this.body.get(row);
	}

	/**
	 * @param msg
	 */
	private void updateColumn(String msg) {
		if (msg.getBytes().length > this.columns) {
			this.columns = msg.getBytes().length;
		}

		for (int row = 0; row < this.body.size(); row++) {
			int fills = this.columns
					- ((String) body.get(row)).getBytes().length;

			if (fills > 0) {
				this.body.set(row, this.body.get(row) + this.spaces(fills));
			}
		}
	}

	/**
	 * 產生空白
	 * 
	 * @param count
	 * @return
	 */
	private String spaces(int count) {
		StringBuffer buf = new StringBuffer();

		for (int i = 0; i < count; i++) {
			buf.append(' ');
		}

		return buf.toString();
	}
}
